package com.jidong.test_springboot.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jidong.test_springboot.entity.Menu;
import com.jidong.test_springboot.module.MenuModule;
import com.jidong.test_springboot.service.MenuService;
import com.jidong.test_springboot.vo.ResultData;

@RestController
@RequestMapping("/menu/")
public class MenuServlet {

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("list.do")
	public ResultData menuList(MenuModule menuMolde) {
		IPage<Menu> page=new Page(menuMolde.getPageNo(),menuMolde.getPageSize());
		QueryWrapper<Menu> qw=new QueryWrapper<>();
		if(menuMolde.getKey()!=null) {
			qw.eq("`key`", menuMolde.getKey());
		}
		if(menuMolde.getName()!=null) {
			qw.eq("`name`", menuMolde.getName());
		}
		if(menuMolde.getComponent()!=null) {
			qw.eq("component", menuMolde.getComponent());
		}
		
		
		
		IPage<Menu> pages=menuService.list(page, qw);
		List<Menu> data=pages.getRecords();
		int pageNo=(int)pages.getCurrent();
		int totalCount=(int)pages.getTotal();
		JSONObject obj=new JSONObject();
		obj.put("data", data);
		obj.put("pageNo", pageNo);
		obj.put("totalCount", totalCount);
		
		ResultData result=new ResultData();
		return result.setCode(200).setMsg("ok").setResult(obj);
	}
}
